package Dao;

import Model.student;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class Dao {
    static Connection conn = null;
    static Statement statement = null;

    static {
        //1.加载驱动
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("C:\\Users\\whb\\lodinweb\\src\\main\\resources\\JDBC.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        //2.获取链接
        try {
            conn = DriverManager.getConnection(url, user, password);
            statement = conn.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Boolean selectone(String name, String pwd) throws SQLException {
        String sql = "select * from login where name='" + name + "' and password='" + pwd + "'";
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet.next();
    }


    public ArrayList<student> select() throws SQLException {
        String sql = "select * from student";
        ResultSet resultSet = statement.executeQuery(sql);

        ArrayList<student> objects = new ArrayList<>();
        while (resultSet.next()) {
            student student = new student();
            student.setId(resultSet.getInt("id"));
            student.setName(resultSet.getString("name"));
            student.setAge(resultSet.getInt("age"));
            student.setHeight(resultSet.getString("height"));
            student.setSchool(resultSet.getString("school"));
            objects.add(student);
        }

        return objects;
    }

    public void add(student o) throws SQLException {
        String sql ="INSERT INTO student(name, age, height, school) VALUES (?, ?, ?, ?)";
        PreparedStatement statement1 = conn.prepareStatement(sql);

        statement1.setString(1,o.getName());
        statement1.setInt(2,o.getAge());
        statement1.setString(3,o.getHeight());
        statement1.setString(4,o.getSchool());
        int i = statement1.executeUpdate();

    }

    public void delete(String id) throws SQLException {
        String sql = "delete from student where id=?";
        Integer id1 = Integer.valueOf(id);
        PreparedStatement statement1 = conn.prepareStatement(sql);
        statement1.setInt(1,id1);
        int i = statement1.executeUpdate();
    }

    public void update(student o) throws SQLException {
        String sql = "update student set name=?,age=?,height=?,school=? where id=?";
        PreparedStatement statement1 = conn.prepareStatement(sql);
        statement1.setString(1,o.getName());
        statement1.setInt(2,o.getAge());
        statement1.setString(3,o.getHeight());
        statement1.setString(4,o.getSchool());
        statement1.setInt(5,o.getId());
        int i = statement1.executeUpdate();
        System.out.println(i);
    }


    public student selectoneDao(Integer id) throws SQLException {
        String sql = "select * from student where id=?";
        PreparedStatement statement1 = conn.prepareStatement(sql);
        statement1.setInt(1,id);
        ResultSet resultSet = statement1.executeQuery();

       resultSet.next();
            student student = new student();
            student.setId(resultSet.getInt("id"));
            student.setName(resultSet.getString("name"));
            student.setAge(resultSet.getInt("age"));
            student.setHeight(resultSet.getString("height"));
            student.setSchool(resultSet.getString("school"));

        return student;
    }
}
